
<!-- 模型 -->
<template>
  <div class="">
    <!-- 顶部搜索部分 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="formInline.messageTitle" placeholder="消息标题" />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="formInline.createTime"
          type="date"
          placeholder="请选择日期"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="search()"
          >查询</el-button
        >
        <el-button
          type="primary"
          plain
          @click="
            dialogSubmitType = 0;
            openUpdate($event, 0);
          "
          >发布消息</el-button
        >
      </el-form-item>

      <!-- 表格 -->
    </el-form>
    <el-table :data="tableData" center border style="width: 100%">
      <el-table-column type="selection" width="50" />
      <el-table-column prop="id" label="编号" />
      <el-table-column prop="messageTitle" label="消息标题" width="180" />
      <el-table-column prop="createTime" label="发布时间" width="240">
        <template #default="{ row }">
          {{ format(row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column prop="username" label="发布人员" />
      <el-table-column label="操作">
        <template #default="s">
          <el-button
            type="primary"
            :data-id="s.row.id"
            :icon="Search"
            @click="openDetail(s.row)"
          />
          <el-button
            type="primary"
            :data-id="s.row.id"
            :icon="Edit"
            @click="
              dialogSubmitType = 1;
              openUpdate($event, 1);
            "
          />
          <el-button
            type="danger"
            :data-id="s.row.id"
            :icon="Delete"
            @click="pubStore.deleteConfirm($event, toDelete)"
          />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="limit"
      :current-page="currentPage"
      :total="total"
      @current-change="pageChange"
    />

    <!-- 添加修改模态框 -->
    <noticeDialog
      ref="noDialog"
      :title="dialogSubmitType == 0 ? '添加公告' : '修改公告'"
      :confirm="dialogSubmitType == 0 ? insertData : updateData"
    >
      <template #main>
        <el-form
          label-position="right"
          label-width="100px"
          :model="addForm"
          style="max-width: 460px"
        >
          <el-form-item label="公告标题：">
            <el-input v-model="addForm.messageTitle" />
          </el-form-item>
          <el-form-item label="公告内容：">
            <el-input type="textarea" :rows="4" v-model="addForm.message" />
          </el-form-item>
        </el-form>
      </template>
    </noticeDialog>

    <!-- 详情模态框 -->
    <defaultDialog ref="deDialog">
      <template #header> {{ formDetail.messageTitle }} </template>
      <template #time> {{ format(formDetail.createTime) }} </template>
      <template #content> {{ formDetail.message }} </template>
    </defaultDialog>
  </div>
</template>

<script setup lang='ts'>
import { ref, reactive, onMounted, toRaw } from "vue";
import { Edit, Delete, Search } from "@element-plus/icons-vue";
import { queryNotice } from "../../service/notice";
import publicStore from "../../store/publicFunction";
import { ElMessage } from "element-plus";
import { noticeHook } from "../../hooks/noticeHooks";
import noticeDialog from "../../components/noticeDialog.vue";
import defaultDialog from "../../components/defaultDialog.vue";

const pubStore = publicStore();
const _noticeHook = noticeHook();

const format = pubStore.formatTime;

onMounted(() => {
  _noticeHook.query(getQueryData(), getData);
  // _noticeHook.select(getData);
});

const getQueryData = () => {
  let queryData = toRaw(formInline);
  // if (queryData.createTime !== null) {
  //   queryData.createTime = new Date(queryData.createTime);

  //   console.log(typeof queryData.createTime);
  // }
  queryData.limit = limit.value;
  queryData.page = currentPage.value;
  return queryData;
};

const search = () => {
  currentPage.value = 1;
  _noticeHook.query(getQueryData(), getData);
};

// 将请求的数据赋值给tableData和total
const getData = (data: any, count: number) => {
  tableData.value = data;
  total.value = count;
};

const tableData = ref<Array<any>>([]);

// 添加模态框的输入表单
const addForm = reactive<queryNotice>({
  messageTitle: "",
  message: "",
});

// 清除添加表单值
const clearAdd = () => {
  (addForm.message = ""), (addForm.messageTitle = "");
};

// 搜索相关的表单
const formInline = reactive<queryNotice>({
  messageTitle: null,
  createTime: null,
});

const formDetail = reactive<queryNotice>({
  messageTitle: "标题",
  message: "内容",
  createTime: "创建时间",
});

const deDialog = ref(null);

const openDetail = (data: any) => {
  formDetail.messageTitle = data.messageTitle;
  formDetail.message = data.message;
  formDetail.createTime = data.createTime;
  deDialog.value.open();
};

const limit = ref<number>(5);
const total = ref<number>(0);
const currentPage = ref<number>(1);

const pageChange = (val: any): void => {
  console.log("值", val);
  currentPage.value = val;
  _noticeHook.query(getQueryData(), getData);
};

// 删除询问框点击确定时调用函数
const toDelete = (id: any) => {
  currentPage.value = 1;
  _noticeHook.deleted(id, getQueryData(), getData);
};

const noDialog = ref(null);
const openUpdate = (e: any, type: number) => {
  let id;
  if (type == 1) {
    id = e.currentTarget.dataset.id;
    _noticeHook.getById(id, function (data: object) {
      Object.assign(addForm, data);
      console.log("addform", addForm);
    });
  } else {
    addForm.messageTitle = "";
    addForm.message = "";
  }

  noDialog.value.open();
};

// 控制添加模态框的表单提交类型
const dialogSubmitType = ref<number>(0);

const insertData = () => {
  let formData = toRaw(addForm);
  let store = sessionStorage.getItem("mainStore");
  let adminId = JSON.parse(store).head.id;
  formData.adminId = adminId;
  _noticeHook.insert(formData, getQueryData(), getData);
  clearAdd();
  // ElMessage("add");
};

const updateData = () => {
  let obj = toRaw(addForm) as queryNotice;
  _noticeHook.update(obj, getQueryData(), getData);
};
</script>

<style scoped>
::v-deep th div,
::v-deep td div {
  text-align: center;
}

::v-deep .el-pagination {
  margin-top: 8px;
  justify-content: flex-end;
}
</style>